<!-- 触发条件 -->

<template>
  <div>
    <MajorTitle title="触发条件" />

    <FormRadio label="触发方式" bind="triggerType" :dict="DICT.TYPE" :disabled="!one.deviceType" required />

    <EasyRow v-if="one.deviceType" col2>
      <FormSelect
        v-if="alarm_readySelect && one.triggerType === 1"
        :label="`${CORE.MAP.ALARM}规则`"
        bind="triggerAlarmCode"
        :dict="alarm_selectDict"
      />
      <FormSelect
        v-else-if="event_readySelect && one.triggerType === 2"
        label="事件类型"
        bind="triggerEventCode"
        :dict="event_selectDict"
      />
      <FormSelect v-else :label="one.triggerType === 1 ? `${CORE.MAP.ALARM}规则` : '事件类型'" :dict="[]" />
    </EasyRow>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyCondition',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [
    CORE.mixWeb(),
    CORE.mixSelect('alarm', 'alarm_'),
    CORE.mixSelect('event', 'event_'),
  ],

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听设备类型
     */
    'one.deviceType': {
      immediate: true,
      handler() {
        this.changeTriggerType()
      },
    },

    /**
     * 监听触发方式
     */
    'one.triggerType': {
      immediate: true,
      handler() {
        this.changeTriggerType()
      },
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 切换设备类型
     */
    changeDeviceType() {
      const { deviceType, triggerType } = this.one
      this.alarm_selectParam.deviceTypeCode = triggerType === 1 ? deviceType : undefined
      this.event_selectParam.deviceTypeCode = triggerType === 2 ? deviceType : undefined
      this.alarm_startSelect()
      this.event_startSelect()
    },

    /**
     * 切换触发方式
     */
    changeTriggerType() {
      const { one } = this
      const emptyKey = one.triggerType === 1 ? 'triggerEventCode' : 'triggerAlarmCode'
      one[emptyKey] = undefined
      this.changeDeviceType()
    },
  },
}
</script>
